package com.gomoku.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gomoku.backend.mapper.UserMapper;
import com.gomoku.backend.pojo.User;
import com.gomoku.backend.service.impl.utils.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService { //实现方法

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {//通过username来返回用户的信息UserDetails

        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        if(user==null){
            //throw new RuntimeException("") //这两好像没啥用
            throw new UsernameNotFoundException("用户不存在");
        }
        return new UserDetailsImpl(user); //需要返回这个类型的东西，实现 `utils.UserDetailsServiceImpl` 类
        //这里新版本登录会报错 There is no PasswordEncoder mapped for the id "null"
        //在数据库密码里面加 {noop}，让他明文传输密码
    }
}